package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.utils.HtmlCleaner;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class XinhuanetNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*[.]xinhuanet.com/.*[0-9]*-[0-9]*/[0-9]*/content_[0-9]+[_0-9]*.htm",
				"http://.*[.]xinhuanet[.]com/.*[0-9]{4}-[0-9]{2}/[0-9]{2}/[a-z]{1}_[0-9]+[_0-9]*.htm",
				"http://.*[.]xinhuanet.com/.*/[0-9]{8}/c_[0-9]+[_0-9]*.htm",
				"http://.*[.]xinhuanet.com/.*/[0-9]{4}-[0-9]{2}-[0-9]{2}/c_[0-9]+[_0-9]*.htm",
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String html=urlMeta.getHtml();
		if ( html== null) {
		}
		String title="";
		String content="";;
		Long date=0l;
		int commentNum=0;
		int clickNum=0;
		NewsMeta meta=new NewsMeta();
		String url=urlMeta.getUrl();
		meta.setUrl(url);
		meta.setType(0);
		Document doc=Jsoup.parse(html);

		doc.select(".news_list_footer").remove();
		doc.select("#news_list_right").remove();
		doc.select("div#div_currpage").remove();
		doc.select("#moreNews").remove();

		urlMeta=null;
		/*
		 * 解析新闻标题
		 */

		title= doc.select("h1#title").text();
		if(StringUtils.isBlank(title)){
			title= doc.select("span#title").text();
			if(StringUtils.isBlank(title)){
				title= doc.select("td.black18").text();
			}
			if(StringUtils.isBlank(title)){
				title= doc.select("title").text();
				if (title!=null) title=StringUtils.substringBefore(title, "_");
			}
		}
		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("#pubtime").text());
		if(date==null){
			date=JSoupUtils.matchDate(doc,"来源：");
			if(date==null){
				date=DateUtils.matchDate(url);
			}
		}
		doc.select("div.hot,div.txlm").remove();
		/*
		 * 解析内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("#news_content p"));
		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("#content,#Content"));
			if (url.startsWith("http://www.he.xinhuanet.com/zfwq/wenwu/")) {
				title= doc.select("table[width=751] tbody tr td[height=40]").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("table[width=751] tbody tr:eq(4)"));
			}else if (url.matches("http://.*yn.xinhuanet.com/.*")) {
				content=HtmlCleaner.getContentHtml(url,doc.select("div#content div#mainText p"));
			}
			if (StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("td.black14"));
			}
			if (StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.article"));
			}
			if (StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
			}
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		String author= JSoupUtils.matchAuthor(doc, "来源：");
		meta.setAuthor(author);
		return meta;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		String url="http://news.xinhuanet.com/politics/2016-04/25/c_1118732222.htm";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		XinhuanetNewsAnalyse cnhubeiNewsAnalyse=new XinhuanetNewsAnalyse();
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if(detailPage){
		NewsMeta parserHtml =cnhubeiNewsAnalyse.parserHtml(urlMeta);
		System.out.println(parserHtml);
		}else{
			System.out.println("不符合正则");
		}
	}



	
	public boolean isNeedUpdate(){
		return false;
	}
}
